package ejbs;

import interfaces.IDepartamento;

import java.io.Serializable;
import java.util.List;


import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import beans.Departamento;

@Stateless
public class DepartamentoEJB implements IDepartamento, Serializable{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@PersistenceContext(unitName = "ProjetoChamadaPU")
	private EntityManager em;
	

	@Override
	public void insert(Departamento d) {
		em.persist(d);
	}

	@Override
	public void delete(int id) {
		Departamento d;
		d = em.find(Departamento.class, id);
		em.remove(d);
		
	}

	@Override
	public void update(Departamento d) {
		em.merge(d);		
	}

	@Override
	public Departamento select(int id) {
		Query qr = em.createNamedQuery("Departamento.findByIddepartamento");
		qr.setParameter("iddepartamento", id);
		Departamento departamento = (Departamento) qr.getSingleResult();
		
		return departamento;
		
	}

	@Override
	public List<Departamento> SelectAll() {
		Query qr = em.createNamedQuery("Departamento.findAll");
		List l =  qr.getResultList();
		return l;
	}

}
